package com.example.shiro.controller;

import com.example.shiro.entity.UserInfo;
import com.example.shiro.service.inter.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@Slf4j
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        System.out.println("UserController.helle()");
        return "ok";
    }

    @GetMapping("/add")
    public String add() {
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        userInfo.getId();
        return "user/add";
    }

    @GetMapping("/add/1")
    public String add1() {
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        userInfo.getId();
        return "user/add1";
    }

    @GetMapping("/add/2")
    public String add2() {
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        userInfo.getId();
        return "user/add2";
    }

    @GetMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @GetMapping("/noAuth")
    public String noAuth() {
        return "noAuth";
    }

    @GetMapping("/update")
    public String update() {
        return "user/update";
    }

    @GetMapping("/testThymeleaf")
    public String testThymeleaf() {
        return "testThymeleaf";
    }

    @GetMapping("/shiro/logout")
    public String logout() {
        SecurityUtils.getSubject().logout();
        return "redirect:/toLogin";
    }

    @GetMapping("/listBySort")
    @ResponseBody
    public UserInfo listBySort(String username) {
        UserInfo userInfos = userInfoService.selectByName(username);
        return userInfos;
    }

    @PostMapping("shiro/login")
    public String login(String name, String password, Model model, HttpSession session, HttpServletResponse res) {
        /**
         * 编写认证操作
         */
        //1.获得subject
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);
        try {
            subject.login(token);
            session.setAttribute("test", "linenxi");
            Cookie cookie = new Cookie("JSESSIONID", session.getId());
            System.err.println(cookie);
            System.err.println(session.getId());
            cookie.setMaxAge(100000);
            res.addCookie(cookie);
            //登入成功
            return "redirect:/testThymeleaf";
        } catch (UnknownAccountException e) {
            //用户名不存在
            model.addAttribute("msg", "用户名不存在");
            return "login";
        } catch (IncorrectCredentialsException e) {
            //用户名不存在
            model.addAttribute("msg", "密码错误");
            return "login";
        }

    }

}
